﻿//--------------------------------------------------------------------
// 动态报表矩形控件 v1.00
//
// 开发人： 朱明智
// 创建时间： 2019-08-22
//
// 修改人：
// 修改时间：
//--------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Dynamic.Controller
{

    /// <summary>
    /// 矩形容器，里面可以放一个或多个Textbox,按行排序
    /// </summary>
    public class Row : ReportController
    {
        private static int index = 0;

        /// <summary>
        /// 控件高 (cm)
        /// </summary>
        public float Height
        {
            get { return _textBoxs.Max(x => x.Height); }
        }

        public float Top
        {
            get { return base.top; }
            internal set { base.top = value; }
        }

        /// <summary>
        /// 布局模式 
        /// </summary>
        public Layout Layout { get; set; }

        private List<TextBox> _textBoxs = new List<TextBox>();

        public Row()
        {
            template = TemplateHelper.RectangleTemp;
            Layout = Layout.EvenlySplit;
        }

        public void AddTextBox(TextBox box)
        {
            _textBoxs.Add(box);
            box.Parent = this;
        }

        protected void CalculationPosition()
        {
            switch (Layout)
            {
                case Layout.EvenlySplit:
                    {
                        float everWidth = this.Report.Width / _textBoxs.Count;
                        float left = 0;

                        for (int i = 0; i < _textBoxs.Count; i++)
                        {
                            var box = _textBoxs[i];
                            box.Width = everWidth;
                            box.Left = left;
                            left += box.Width;
                        }
                    }
                    break;
                case Layout.RightToLeft:
                    {
                        float left = 0;
                        for (int i = 0; i < _textBoxs.Count; i++)
                        {
                            var box = _textBoxs[i];
                            box.Left = left;
                            if (left + box.Width >= this.Report.Width)
                            {
                                box.Width = this.Report.Width - left;
                                left = this.Report.Width;
                            }
                            else
                            {
                                left += box.Width;
                            }
                        }
                    }
                    break;
                case Layout.LeftToRight:
                    {
                        float left = this.Report.Width;
                        for (int i = 0; i < _textBoxs.Count; i++)
                        {
                            var box = _textBoxs[i];

                            if (left - box.Width <= 0)
                            {
                                box.Width = left;
                                box.Left = left = 0;
                            }
                            else
                            {
                                left -= box.Width;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
           
        }

        public override string ToString()
        {
            CalculationPosition();

            string str = base.ToString().Replace("@Name", (index++).ToString());
            foreach (var box in _textBoxs)
            {
                if (_textBoxs.IndexOf(box) == _textBoxs.Count - 1)
                    str = str.Replace("@Textbox", box.GetTempStr());
                else
                    str = str.Insert(str.IndexOf("@Textbox"), box.GetTempStr());
            }

            if (_textBoxs.Count == 0)
                str = str.Replace("<ReportItems></ReportItems", "");
            return str;
        }

     
    }
}